/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import fr.cggveritas.crewintranet.jms.entities.CvListeposte;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvPosteoccupe;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvListeposteJpaController extends DaoImp implements Serializable {

    public CvListeposteJpaController() {
        super();
    }

    public void create(CvListeposte cvListeposte) throws PreexistingEntityException, Exception {
        if (cvListeposte.getCvPosteoccupeList() == null) {
            cvListeposte.setCvPosteoccupeList(new ArrayList<CvPosteoccupe>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<CvPosteoccupe> attachedCvPosteoccupeList = new ArrayList<CvPosteoccupe>();
            for (CvPosteoccupe cvPosteoccupeListCvPosteoccupeToAttach : cvListeposte.getCvPosteoccupeList()) {
                cvPosteoccupeListCvPosteoccupeToAttach = em.getReference(cvPosteoccupeListCvPosteoccupeToAttach.getClass(), cvPosteoccupeListCvPosteoccupeToAttach.getIdposteoccupe());
                attachedCvPosteoccupeList.add(cvPosteoccupeListCvPosteoccupeToAttach);
            }
            cvListeposte.setCvPosteoccupeList(attachedCvPosteoccupeList);
            em.persist(cvListeposte);
            for (CvPosteoccupe cvPosteoccupeListCvPosteoccupe : cvListeposte.getCvPosteoccupeList()) {
                CvListeposte oldCvListeposteIdposteOfCvPosteoccupeListCvPosteoccupe = cvPosteoccupeListCvPosteoccupe.getCvListeposteIdposte();
                cvPosteoccupeListCvPosteoccupe.setCvListeposteIdposte(cvListeposte);
                cvPosteoccupeListCvPosteoccupe = em.merge(cvPosteoccupeListCvPosteoccupe);
                if (oldCvListeposteIdposteOfCvPosteoccupeListCvPosteoccupe != null) {
                    oldCvListeposteIdposteOfCvPosteoccupeListCvPosteoccupe.getCvPosteoccupeList().remove(cvPosteoccupeListCvPosteoccupe);
                    oldCvListeposteIdposteOfCvPosteoccupeListCvPosteoccupe = em.merge(oldCvListeposteIdposteOfCvPosteoccupeListCvPosteoccupe);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCvListeposte(cvListeposte.getIdposte()) != null) {
                throw new PreexistingEntityException("CvListeposte " + cvListeposte + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvListeposte cvListeposte) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvListeposte persistentCvListeposte = em.find(CvListeposte.class, cvListeposte.getIdposte());
            List<CvPosteoccupe> cvPosteoccupeListOld = persistentCvListeposte.getCvPosteoccupeList();
            List<CvPosteoccupe> cvPosteoccupeListNew = cvListeposte.getCvPosteoccupeList();
            List<String> illegalOrphanMessages = null;
            for (CvPosteoccupe cvPosteoccupeListOldCvPosteoccupe : cvPosteoccupeListOld) {
                if (!cvPosteoccupeListNew.contains(cvPosteoccupeListOldCvPosteoccupe)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvPosteoccupe " + cvPosteoccupeListOldCvPosteoccupe + " since its cvListeposteIdposte field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<CvPosteoccupe> attachedCvPosteoccupeListNew = new ArrayList<CvPosteoccupe>();
            for (CvPosteoccupe cvPosteoccupeListNewCvPosteoccupeToAttach : cvPosteoccupeListNew) {
                cvPosteoccupeListNewCvPosteoccupeToAttach = em.getReference(cvPosteoccupeListNewCvPosteoccupeToAttach.getClass(), cvPosteoccupeListNewCvPosteoccupeToAttach.getIdposteoccupe());
                attachedCvPosteoccupeListNew.add(cvPosteoccupeListNewCvPosteoccupeToAttach);
            }
            cvPosteoccupeListNew = attachedCvPosteoccupeListNew;
            cvListeposte.setCvPosteoccupeList(cvPosteoccupeListNew);
            cvListeposte = em.merge(cvListeposte);
            for (CvPosteoccupe cvPosteoccupeListNewCvPosteoccupe : cvPosteoccupeListNew) {
                if (!cvPosteoccupeListOld.contains(cvPosteoccupeListNewCvPosteoccupe)) {
                    CvListeposte oldCvListeposteIdposteOfCvPosteoccupeListNewCvPosteoccupe = cvPosteoccupeListNewCvPosteoccupe.getCvListeposteIdposte();
                    cvPosteoccupeListNewCvPosteoccupe.setCvListeposteIdposte(cvListeposte);
                    cvPosteoccupeListNewCvPosteoccupe = em.merge(cvPosteoccupeListNewCvPosteoccupe);
                    if (oldCvListeposteIdposteOfCvPosteoccupeListNewCvPosteoccupe != null && !oldCvListeposteIdposteOfCvPosteoccupeListNewCvPosteoccupe.equals(cvListeposte)) {
                        oldCvListeposteIdposteOfCvPosteoccupeListNewCvPosteoccupe.getCvPosteoccupeList().remove(cvPosteoccupeListNewCvPosteoccupe);
                        oldCvListeposteIdposteOfCvPosteoccupeListNewCvPosteoccupe = em.merge(oldCvListeposteIdposteOfCvPosteoccupeListNewCvPosteoccupe);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = cvListeposte.getIdposte();
                if (findCvListeposte(id) == null) {
                    throw new NonexistentEntityException("The cvListeposte with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvListeposte cvListeposte;
            try {
                cvListeposte = em.getReference(CvListeposte.class, id);
                cvListeposte.getIdposte();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvListeposte with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CvPosteoccupe> cvPosteoccupeListOrphanCheck = cvListeposte.getCvPosteoccupeList();
            for (CvPosteoccupe cvPosteoccupeListOrphanCheckCvPosteoccupe : cvPosteoccupeListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvListeposte (" + cvListeposte + ") cannot be destroyed since the CvPosteoccupe " + cvPosteoccupeListOrphanCheckCvPosteoccupe + " in its cvPosteoccupeList field has a non-nullable cvListeposteIdposte field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(cvListeposte);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvListeposte> findCvListeposteEntities() {
        return findCvListeposteEntities(true, -1, -1);
    }

    public List<CvListeposte> findCvListeposteEntitiesByACtive(boolean etat) {

        EntityManager em = getEntityManager();
        try {

            List<CvListeposte> listCvListepostes = new ArrayList<CvListeposte>();
            Query query = em.createNamedQuery("CvListeposte.findByActive");
            List result = query.setParameter("active", etat).getResultList();
            if (!result.isEmpty()) {
                return result;
            }
            return listCvListepostes;

        } finally {
            em.close();
        }

    }

    public List<CvListeposte> findCvListeposteEntities(int maxResults, int firstResult) {
        return findCvListeposteEntities(false, maxResults, firstResult);
    }

    private List<CvListeposte> findCvListeposteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvListeposte.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvListeposte findCvListeposte(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvListeposte.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvListeposteCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvListeposte> rt = cq.from(CvListeposte.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
